package streamforce.workload.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

public class JoinCompare {
	public static void main(String[] args) throws Exception{
		
		Set<TupleSetElement> tupleSet1 = readFile(args[0]);
		Set<TupleSetElement> tupleSet2 = readFile(args[1]); 
		System.out.println("s1: "+tupleSet1.size()+" s2: "+tupleSet2.size()); 
		System.out.println(compareSets(tupleSet1, tupleSet2)); 
	}
	
	public static int compareSets(Set<TupleSetElement> set1, Set<TupleSetElement> set2){
		Set<TupleSetElement> set1dup = new HashSet<TupleSetElement>();
		for (TupleSetElement s: set1)
			set1dup.add(s); 
		set1.removeAll(set2); 
		set2.removeAll(set1dup);
		
		return set1.size()+set2.size(); 
	}
	
	public static Set<TupleSetElement> readFile(String file) throws Exception{ 
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = reader.readLine();
		Set<TupleSetElement> tupleSet = new HashSet<TupleSetElement>();
		int idx0, idx1, val0, val1; 		
		do{
			
			String[] ss = line.split(" "); 
			val0 = new Integer(ss[1]).intValue(); 
			idx0 = new Integer(ss[0]).intValue(); 
			val1 = new Integer(ss[3]).intValue(); 
			idx1 = new Integer(ss[2]).intValue();
			//System.out.format("read %d, %d, %d, %d\n", idx0, val0, idx1, val1);
			if (idx0==0)
				tupleSet.add(new TupleSetElement(val0,val1));
			else
				tupleSet.add(new TupleSetElement(val1,val0)); 
			line = reader.readLine(); 
			
		}while (line!=null);
		reader.close(); 
		return tupleSet; 
	}
}

class TupleSetElement{
	int ts0, ts1; 
	public TupleSetElement(int ts0, int ts1){
		this.ts0 = ts0; 
		this.ts1 = ts1; 
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ts0;
		result = prime * result + ts1;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TupleSetElement other = (TupleSetElement) obj;
		if (ts0 != other.ts0)
			return false;
		if (ts1 != other.ts1)
			return false;
		return true;
	}
	
	
}
